package com.gem.service.impl;

import com.gem.domain.Campus;
import com.gem.domain.CampusConn;
import com.gem.domain.CampusInfo;
import com.gem.mapper.CampusMapper;
import com.gem.service.CampusService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author: 玲
 * @Description:
 * @create 2019-10-16 11:26
 * @Modified By:
 */
@Service
@SuppressWarnings("all")
@Transactional(rollbackFor=Exception.class)
public class CampusServiceImpl implements CampusService {

    @Autowired
    private CampusMapper campusMapper;


    @Override
    public PageInfo<Campus> findAll(int currentPage, int pageSize) {
        PageInfo<Campus> pageInfo = null;
        try {
            PageHelper.startPage(currentPage, pageSize);
            pageInfo = new PageInfo<>(campusMapper.findAll());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public Campus findById(Integer campusId) {
        return campusMapper.findById(campusId);
    }

    @Override
    public void save(Campus campus, CampusInfo info) {
        campus.setCampusCreateDate(new Date());
        campusMapper.save(campus);
        if ( !"".equals(info.getCampusInfoProfessionSituation()) && info.getCampusInfoProfessionSituation() != null ){
            info.setCampusId(campus.getCampusId());
            info.setCampusName(campus.getCampusName());
            campusMapper.saveInfo(info);
        }
    }

    @Override
    public Integer deleteById(Integer campusId) {
        return campusMapper.deleteById(campusId);
    }

    @Override
    public Integer update(Campus campus) {
        return campusMapper.update(campus);
    }

    @Override
    public PageInfo<Campus> search(Campus campus, int currentPage, int pageSize) {
        if (campus.getCampusName() != null) {
            if ("null".equals(campus.getCampusName()) || campus.getCampusName().length() == 0) {
                campus.setCampusName(null);
            } else {
                campus.setCampusName(campus.getCampusName().replaceAll(" ", ""));
                campus.setCampusName("%" + campus.getCampusName() + "%");
            }
        }
        if (campus.getPersonname() != null) {
            if ("null".equals(campus.getPersonname()) || campus.getPersonname().length() == 0) {
                campus.setPersonname(null);
            } else {
                campus.setPersonname(campus.getPersonname().replaceAll(" ", ""));
                campus.setPersonname("%" + campus.getPersonname() + "%");
            }
        }
        PageInfo<Campus> pageInfo = null;
        try {
            PageHelper.startPage(currentPage, pageSize);
            pageInfo = new PageInfo<>(campusMapper.search(campus));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public CampusInfo findInfo(Integer campusId) {
        return campusMapper.findInfo(campusId);
    }

    @Override
    public Integer updateInfo(CampusInfo info) {
        return campusMapper.updateInfo(info);
    }

    @Override
    public Integer saveInfo(CampusInfo info) {
        return campusMapper.saveInfo(info);
    }

    @Override
    public List<Integer> getAllSellers() {
        return campusMapper.getAllSellers();
    }

    @Override
    public List<Integer> getCampusIds(Integer allSeller) {
        return campusMapper.getCampusIds(allSeller);
    }

    @Override

    public Integer deleteAll(int[] campusIds) {
        Integer result = campusMapper.deleteAll(campusIds);
        return result;
    }

    @Override
    public List<Campus> findAllCampus() {
        return campusMapper.findAll();
    }

    @Override
    public Integer deleteInfo(Integer campusInfoId) {
        return campusMapper.deleteInfo(campusInfoId);
    }

    @Override
    public List<CampusInfo> findAllInfo() {
        return campusMapper.findAllInfo();
    }

    @Override
    public Integer saveExcel(Campus campus) {
        return campusMapper.save(campus);
    }


}
